package Test03;

import java.util.Calendar;

public class CalculateTimeSpan {
    private Calendar starttime;
    private Calendar finishtime;
    private int weekdays = 7;
    private int minmonth = 1;
    private int maxmonth = 12;
    private int minue = 59;
    private int seconds = 60;
    private int maxhour = 30;

    public CalculateTimeSpan(final Calendar date1, final Calendar date2) {
        starttime = date1;
        finishtime = date2;
    }

    //得到当天是本周的第几天
    public int getWeekDay(int year, int month, final int day) {
        int weekday;
        for (; month <= minmonth - 1 || month > maxmonth;) {
            if (month <= minmonth) {
                year -= minmonth;
                month += maxmonth;
            } else if (month > maxmonth) {
                year += minmonth;
                month -= maxmonth;
            }
        }
        weekday = year > 0 ? (5 + (year + 1) + (year - 1) / 4 - (year - 1) / 100
                + (year - 1) / 400) % weekdays : (5 + year + year / 4 - year / 100 + year / 400) % weekdays;
        weekday = month > 2 ? (weekday + 2 * (month + 1) + 3 * (month + 1) / 5) % weekdays
                : (weekday + 2 * (month + 2) + 3 * (month + 2) / 5) % weekdays;
        if (((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) && month > 2) {
            weekday = (weekday + 1) % weekdays;
        }
        weekday = (weekday + day) % weekdays;
        return weekday;
    }
    //计算通话时长
    public int judge() {
        float totalsecond;
        int totalminiute;
        boolean issummer1 = false;
        boolean issummer2 = false;
        boolean isaut1 = false;
        boolean isaut2 = false;

        int sy = starttime.get(Calendar.YEAR); //年
        int smonth = starttime.get(Calendar.MONTH) + 1; //月
        int sd = starttime.get(Calendar.DATE);       //日
        int sh = starttime.get(Calendar.HOUR_OF_DAY);
        int sm = starttime.get(Calendar.MINUTE);
        int ss = starttime.get(Calendar.SECOND);

        int ey = finishtime.get(Calendar.YEAR); //年
        int emonth = finishtime.get(Calendar.MONTH) + 1; //月
        int ed = finishtime.get(Calendar.DATE);       //日
        int eh = finishtime.get(Calendar.HOUR_OF_DAY);
        int em = finishtime.get(Calendar.MINUTE);
        int es = finishtime.get(Calendar.SECOND);
        //夏令时
        if (smonth == 3 && getWeekDay(sy, smonth, sd) == 0) {
            System.out.println("今天是三月的一个星期天！！");
            if (sh >= 2) {
                System.out.println("拨打电话时间已是夏令时！");
                issummer1 = true;
                sh = sh + 1;
            }
        } else if (smonth == 11 && getWeekDay(sy, smonth, sd) == 0 && sd < 8) {
            System.out.println("今天是十一月的第一个星期天");
            if (sh == 2 && sm == 59 && ss == 59) {
                System.out.println("拨打电话时间已是秋令时！");
                isaut1 = true;
                sh = 2;
                sm = 0;
                ss = 0;
            } else if (sh >= 3) {
                System.out.println("拨打电话时间已是秋令时！");
                isaut1 = true;
                sm = sm - 59;
                ss = ss - 59;
            }
        } //秋令时
        //夏令时
        if (emonth == 3 && getWeekDay(ey, emonth, ed) == 0) {
            if (eh >= 2) {
                System.out.println("结束电话时间已是夏令时");
                issummer2 = true;
                eh = eh + 1;
            }
        } else if (emonth == 11 && getWeekDay(ey, emonth, ed) == 0 && ed < 8) {
            if (eh == 2 && em == 59 && es == 59) {
                System.out.println("结束电话时间已是秋令时！");
                isaut2 = true;
                eh = 2;
                em = 0;
                es = 0;
            } else if (eh >= 3) {
                System.out.println("拨打电话时间已是秋令时！");
                isaut2 = true;
                em = em - 59;
                es = es - 59;
            }
        } //秋令时
        int i;
        for (i = 0; i < 10; i++) {
            if (getWeekDay(sy, 3, 26 + i) == 0) {
                if (i == 0 || i == 1) {
                    continue;
                } else {
                    break;
                }
            }
        }
        int j;
        for (j = 7; j > 0; j--) {
            if (getWeekDay(sy, 10, 31 - j) == 0) {
                break;
            }
        }

        if (ed > sd) { //计算两个日期实际相差时长
            totalsecond = 24 * seconds * seconds - (sh * seconds * seconds + sm * seconds + ss)
                    + eh * seconds * seconds + em * seconds + es;
        } else {
            totalsecond = (eh * seconds * seconds + em * seconds + es) - (sh * seconds * seconds + sm * seconds + ss);
        }

        if (isaut1 == false && isaut2 == true) {
            totalsecond = totalsecond + 59 + 59 * 60;
        }
        if (issummer1 == false && issummer2 == true) {
            totalsecond = totalsecond - 1 * 60 * 60;
        }

        float haha = totalsecond % seconds; // 秒数取余，

        if (haha > 0.0) {
            totalminiute = (int) (totalsecond / seconds) + 1;
        } else {
            totalminiute = (int) (totalsecond / seconds);
        }


        //通话时长大于30小时
        int a = (int) (totalminiute / seconds); //计算通话时长，向下取整，一个有多少小时
        if (a > maxhour) { //通话时长大于30小时，返回0，因为计算出来的分钟不会出现0，“不满1分钟按1min计算”原则
            return 0;
        }
        return totalminiute;
    }
}


